﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CoreUC
{
    /// <summary>
    /// Login form control
    /// </summary>
    /// <remarks>trungpv@vnext.vn</remarks>
    public partial class LoginForm : DevComponents.DotNetBar.Office2007Form
    {
        #region Attributs and Getter Setter
        private string m_password;
        private bool m_isAdmin;

        public bool IsAdmin
        {
            get { return m_isAdmin; }
            set { m_isAdmin = value; }
        }

        public string Password
        {
            get { return m_password; }
            set { m_password = value; }
        }

        #endregion

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="password"></param>
        public LoginForm(string password)
        {
            InitializeComponent();
            m_password = password;
        }

        private void LoginForm_Load(object sender, EventArgs e)
        {
            tbPassword.Select(); // Focus on password TextBox
        }

        #region Logic functions
        /// <summary>
        /// Check entered password
        /// </summary>
        /// <returns></returns>
        private bool checkPassword()
        {
            return (m_password == tbPassword.Text);
        }

        /// <summary>
        /// Start login
        /// </summary>
        private void login()
        {
            if (checkPassword())
            {
                m_isAdmin = true;
                this.Close();
            }
            else
            {
                tbPassword.Text = "";
                tbPassword.Select();
                System.Windows.Forms.MessageBox.Show(Messages.WRONG_PASSWORD, Messages.NOTE,
                    MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
        #endregion

        #region Events on form
        /// <summary>
        /// Escape login form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// Login 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            login();
        }

        /// <summary>
        /// Pressed enter key
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13) // Enter key
                login();
        }
        #endregion
    }
}
